package com.qp.basics.estivate.jdbc.datasource;

import com.qp.basics.estivate.Database;
import com.qp.basics.estivate.Session;
import org.springframework.transaction.support.TransactionSynchronizationManager;

public class SessionUtils {

	public static Session getSession(Database database) {
		SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
				.getResource(database);
		if (sessionHolder != null)
			return sessionHolder.getSession();

		Session session = database.openSession();

		return session;
	}

	public static void releaseSession(Session session, Database database) {
		if (null == session) {
			return;
		}

		if (null != database) {
			SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
					.getResource(database);
			if (null != sessionHolder) {
				return;
			}
		}

		session.close();

	}
}
